package com.gc.server.controller.api;

import com.gc.server.entity.RouteEntity;
import com.gc.server.entity.ShopEntity;
import com.gc.server.entity.UserEntity;
import com.gc.server.entity.UserShopEntity;
import com.gc.server.service.RouteService;
import com.gc.server.service.ShopService;
import com.gc.server.service.UserShopService;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/shop")
public class ApiShopController {

    @Autowired
    private ShopService shopService;

    /**
     * 某人药店列表
     * @param userId
     * @return
     */
    @GetMapping("/list/user")
    @IgnoreAuth
    public R listByUserId(Long userId){
        List<ShopEntity> list = shopService.findByUserId(userId);
        return R.ok(list);
    }

    @Autowired
    private UserShopService userShopService;

    @GetMapping("/id")
    public R getShop(@LoginUserId Long userId,Long id){
        ShopEntity shopEntity = shopService.get(id);
        List<UserEntity> users = userShopService.findUserByShopId(id);
        //只需要我的下级
        users = users.stream().filter(user->user.getPid().equals(userId)).collect(Collectors.toList());
        return R.ok(shopEntity).putExtra("user",users);
    }


    @PostMapping("/add")
    public R addShop(@LoginUserId Long userId,@RequestBody ShopEntity shopEntity){
        shopService.add(userId,shopEntity);
        return R.ok();
    }

    /**
     * 某人药店列表
     * @param userId
     * @return
     */
    @GetMapping("/list")
    public R list(@LoginUserId Long userId){
        List<ShopEntity> list = shopService.findByUserId(userId);
        return R.ok(list);
    }


    @GetMapping("/route")
    public R listByRote(Long routeId){
        List<ShopEntity> list = shopService.findByRouteId(routeId);
        return R.ok(list);
    }



}
